<template>
  <div class='xtx-carousel' @mouseleave="start" @mouseenter="stop">
    <ul class="carousel-body">
      <li class="carousel-item" v-for="(item,i) in sliders" :key="i" :class="{fade:index===i}">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="">
        </RouterLink>
      </li>
    </ul>
    <a @click="toggle(true)" href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="toggle(false)" href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <!-- 点击哪个小圆点，去哪张图片 index=i -->
      <span @click="index=i" v-for="(item,i) in sliders" :key="i" :class="{active:index===i}"></span>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 接收父组件传来轮播图数据
    sliders: {
      type: Array,
      default: () => []
    },
    // 设置是否开启自动轮播
    autoplay: {
      type: Boolean,
      default: true
    },
    // 设置轮播图间隔事件
    duration: {
      type: Number,
      default: 2
    }
  },
  setup (props) {
    // 默认显示的图片的索引
    const index = ref(0)
    // 自动播放
    // 设置定时器初始值
    let timer = null
    const autoplayFn = () => {
      // 在启用之前先清空定时器
      clearInterval(timer)
      timer = setInterval(() => {
        // 轮播图索引++
        index.value++
        // 默认图片的索引大于数组长度时，索引返回初始值 0
        if (index.value >= props.sliders.length) {
          index.value = 0
        }
      }, props.duration * 1000)
    }

    // 启动
    const start = () => {
      if (props.sliders.length > 1 && props.autoplay) {
        autoplayFn()
      }
    }
    // 暂停
    const stop = () => {
      if (timer) clearInterval(timer)
    }
    // 深度监听
    watch(() => props.sliders, (newVal) => {
      if (newVal.length > 1 && props.autoplay) {
        index.value = 0
        start()
      }
    })
    const toggle = (type) => {
      if (type) {
        // 如果type时ture，点击向左移动按钮
        if (index.value >= 1) {
          // 向左移动
          index.value--
        } else {
          // 到达最左侧时，返回最右侧
          index.value = props.sliders.length - 1
        }
      } else {
        // 点击向右侧移动按钮
        if (index.value < props.sliders.length - 1) {
          index.value++
        } else {
          // 到达最右侧时，返回最左侧
          index.value = 0
        }
      }
    }
    return { index, start, stop, toggle }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
